﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Fight.userControls;

namespace Fight
{
    public partial class gameStage : UserControl
    {
        public gameStage(general.gameMode gameMode)
        {
            InitializeComponent();


            switch(gameMode){
                case general.gameMode.multiplayerNetwork: ; break;
                case general.gameMode.multiplayerSameComputer: ; break;
                case general.gameMode.singlePlayer: ; break;
                default: throw new NotImplementedException();
            }
            general.screenWidth = App.Current.Host.Content.ActualWidth;
            general.screenHeight = App.Current.Host.Content.ActualHeight;
            general.groundHeightTopDistance = general.screenHeight - general.fighterHightPlusMarginFromGround;

            Canvas.SetLeft(spLeftPlayer, general.leftMarginForLeftFighterOnGameStart);
            Canvas.SetTop(spLeftPlayer, general.groundHeightTopDistance);

            Canvas.SetLeft(spRightPlayer, general.screenWidth - general.rightMarginForRightFighterOnGameStart);
            Canvas.SetTop(spRightPlayer, general.groundHeightTopDistance);

            general.leftFighter = new fighter(general.fighterSide.leftPlayer);
            general.rightFighter = new fighter(general.fighterSide.rightPlayer);
            

            spLeftPlayer.Children.Add(general.leftFighter);
            spRightPlayer.Children.Add(general.rightFighter);


            
            tbEvents.Focus();
            tbEvents.LostFocus += new RoutedEventHandler(tbEvents_LostFocus);

            tbEvents.KeyDown += new KeyEventHandler(gameStage_KeyDown);
        }

        void tbEvents_LostFocus(object sender, RoutedEventArgs e)
        {
            tbEvents.Focus();
        }

        void gameStage_KeyDown(object sender, KeyEventArgs e)
        {
            tbEvents.Text = "";
            switch (e.Key)
            {
                case (firstPlayerMove.keyMoveRight): if (firstPlayerMove.side == general.fighterSide.leftPlayer) moveRight(spLeftPlayer, LayoutRoot); else moveRight(spRightPlayer, LayoutRoot); break;
                case (firstPlayerMove.keyMoveLeft): if (firstPlayerMove.side == general.fighterSide.leftPlayer) moveLeft(spLeftPlayer, LayoutRoot); else moveLeft(spRightPlayer, LayoutRoot); break;
                case (secondPlayerMove.keyMoveRight): if (secondPlayerMove.side == general.fighterSide.leftPlayer) moveRight(spLeftPlayer, LayoutRoot); else moveRight(spRightPlayer, LayoutRoot); break;
                case (secondPlayerMove.keyMoveLeft): if (secondPlayerMove.side == general.fighterSide.leftPlayer) moveLeft(spLeftPlayer, LayoutRoot); else moveLeft(spRightPlayer, LayoutRoot); break;
                case (firstPlayerMove.keyJump): if (firstPlayerMove.side == general.fighterSide.leftPlayer) moveUp(spLeftPlayer, LayoutRoot); else moveUp(spRightPlayer, LayoutRoot); break;
                case (secondPlayerMove.keyJump): if (secondPlayerMove.side == general.fighterSide.leftPlayer) moveUp(spLeftPlayer, LayoutRoot); else moveUp(spRightPlayer, LayoutRoot); break;
               

                default: ; break;
            }
        }

        private void moveUp(StackPanel dp, Canvas cv)
        {
            Duration duration = new Duration(TimeSpan.FromMilliseconds(general.moveUpAnimationDuration));
            DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
            myDoubleAnimation1.Duration = duration;
         
            myDoubleAnimation1.From = Canvas.GetTop((UIElement)dp);
            myDoubleAnimation1.To = Canvas.GetTop((UIElement)dp) - general.moveUpAnimationDistance;

            if (myDoubleAnimation1.From == general.groundHeightTopDistance)
            {
                Storyboard sb = new Storyboard();
                sb.Duration = duration;
                sb.Children.Add(myDoubleAnimation1);
                sb.AutoReverse = true;

                Storyboard.SetTarget(myDoubleAnimation1, dp);
                Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(Canvas.Top)"));
                try
                {
                    cv.Resources.Remove("moveUp");
                }
                catch { }
                cv.Resources.Add("moveUp", sb);

                sb.Begin();
            }
        }

        private void moveLeft(DependencyObject dp, Canvas cv)
        {
            Duration duration = new Duration(TimeSpan.FromMilliseconds(general.moveLeftAndRightAnimationDuration));
            DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
            myDoubleAnimation1.Duration = duration;

            myDoubleAnimation1.From = Canvas.GetLeft((UIElement)dp);
            myDoubleAnimation1.To = Canvas.GetLeft((UIElement)dp) - general.moveLeftAndRightAnimationDistance;

            if (myDoubleAnimation1.To < general.leftMarginForLeftFighterOnGameStart)// if the movement distance overlap the margin 
            {
                myDoubleAnimation1.To = general.leftMarginForLeftFighterOnGameStart;
                // to bring down the duration in the right ratio
                double realDuration = (((double)myDoubleAnimation1.From - (double)myDoubleAnimation1.To) * general.moveLeftAndRightAnimationDuration) / general.moveLeftAndRightAnimationDistance;
                duration = new Duration(TimeSpan.FromMilliseconds(realDuration));
                myDoubleAnimation1.Duration = duration;
            }

            Storyboard sb = new Storyboard();
            sb.Duration = duration;
            sb.Children.Add(myDoubleAnimation1);

            Storyboard.SetTarget(myDoubleAnimation1, dp);
            Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(Canvas.Left)"));
            try
            {
                cv.Resources.Remove("moveLeftRight");
            }
            catch { }
            cv.Resources.Add("moveLeftRight", sb);

            sb.Begin();
           
        }


        void initiateGameStage()
        {
        }


        public void moveRight(DependencyObject dp, Canvas cv)
        {
            Duration duration = new Duration(TimeSpan.FromMilliseconds(general.moveLeftAndRightAnimationDuration));
            DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
            myDoubleAnimation1.Duration = duration;
            
            myDoubleAnimation1.From = Canvas.GetLeft((UIElement)dp);
            myDoubleAnimation1.To = Canvas.GetLeft((UIElement)dp) + general.moveLeftAndRightAnimationDistance;

            if (myDoubleAnimation1.To > general.screenWidth - general.rightMarginForRightFighterOnGameStart)
            {
                myDoubleAnimation1.To = general.screenWidth - general.rightMarginForRightFighterOnGameStart;
                // to bring down the duration in the right ratio
                double realDuration = (((double)myDoubleAnimation1.To - ((double)myDoubleAnimation1.From)) * general.moveLeftAndRightAnimationDuration) / general.moveLeftAndRightAnimationDistance;
                duration = new Duration(TimeSpan.FromMilliseconds(realDuration));
                myDoubleAnimation1.Duration = duration;
            }

            Storyboard sb = new Storyboard();
            sb.Duration = duration;
            sb.Children.Add(myDoubleAnimation1);

            Storyboard.SetTarget(myDoubleAnimation1, dp);
            Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(Canvas.Left)"));
            try
            {
                cv.Resources.Remove("moveLeftRight");
            }
            catch { }
            cv.Resources.Add("moveLeftRight", sb);

            sb.Begin();
        }
    }





}
